<?php

namespace Admin\Form;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\Form\Form;
use Zend\Form\Element;
use Base\Form\ProvidesEventsForm;


class Base extends ProvidesEventsForm implements ServiceManagerAwareInterface
{
    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    protected $rolesMapper;

    public function __construct($name , $roleMapper)
    {

        parent::__construct();
        $this->rolesMapper = $roleMapper;
        
        $this->add(array(
            'name' => 'firstname',
            'options' => array(
                'label' => 'First name',
            ),
            'attributes' => array(
                'type' => 'text',
                'class' => 'form-control col-xs-12',
                'placeholder' => 'First name'
            ),
        ));

        $this->add(array(
            'name' => 'lastname',
            'options' => array(
                'label' => 'Last name',
            ),
            'attributes' => array(
                'type' => 'text',
                'class' => 'form-control col-xs-12',
                'placeholder' => 'Last name'
            ),
        ));


        $this->add(array(
            'name' => 'email',
            'options' => array(
                'label' => 'Email',
            ),
            'attributes' => array(
                'type' => 'text',
                'class' => 'form-control col-xs-12',
                'placeholder' => 'Email'
            ),
        ));

        $this->add(array(
            'name' => 'phone',
            'options' => array(
                'label' => 'Phone',
            ),
            'attributes' => array(
                'type' => 'text',
                'class' => 'form-control col-xs-12',
                'placeholder' => 'Phone'
            ),
        ));

        $this->add(array(
            'name' => 'textarea',
            'options' => array(
                'label' => 'address',
            ),
            'attributes' => array(
                'type' => 'textarea',
                'class' => 'form-control col-xs-12',
                'placeholder' => 'address'
            ),
        ));

        /*
        $this->add(array(
            'type' => 'select',
            'name' => 'role_id',
            'required' => true ,
            'options' => array(
                'label' => 'Role',
                'value_options' => $this->getOptionsForRoles(),
            ),
            'attributes' => array(
                'class' => 'form-control col-xs-12',
                'placeholder' => 'Role'
            ),
        ));*/


        $this->add(array(
            'name' => 'password',
            'options' => array(
                'label' => 'Password',
            ),
            'attributes' => array(
                'type' => 'password',
                'class' => 'form-control col-xs-12',
                'placeholder' => 'password'
            ),
        ));

        $this->add(array(
            'name' => 'password_verify',
            'options' => array(
                'label' => 'Password Verify',
            ),
            'attributes' => array(
                'type' => 'password',
                'class' => 'form-control col-xs-12',
                'placeholder' => 'password verify'
            ),
        ));

        /*
        if ($this->getRegistrationoptions()->getUseRegistrationFormCaptcha()) {
            $this->add(array(
                'name' => 'captcha',
                'type' => 'Zend\Form\Element\Captcha',
                'options' => array(
                    'label' => 'Please type the following text',
                    'captcha' => $this->getRegistrationoptions()->getFormCaptchaoptions(),
                ),
            ));
        }*/

        $submitElement = new Element\Button('submit');
        $submitElement
            ->setLabel('Submit')
            ->setAttributes(array(
                'type'  => 'submit',
                'class' => 'btn btn-primary'
            ));

        $this->add($submitElement, array(
            'priority' => -100,
        ));

        $this->add(array(
            'name' => 'user_id',
            'type' => 'Zend\Form\Element\Hidden',
            'attributes' => array(
                'type' => 'hidden'
            ),
        ));

        $this->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'csrf',
            'options' => array(
                'csrf_options' => array(
                    'timeout' => 600
                )
            )
        ));
    }

    public function getOptionsForRoles()
    {
        $roles = $this->rolesMapper->fetchAll();
        $arrayRoles = array();
        foreach($roles as $role)
        {
            $arrayRoles[$role->getId()] = $role->getRoleId();
        }
        return $arrayRoles;
    }
    
    /**
     * @param mixed $rolesMapper
     */
    public function setRolesMapper(\RoleUser\Mapper\RoleMapper $rolesMapper)
    {
        $this->rolesMapper = $rolesMapper;
    }

    /**
     * @return mixed
     */
    public function getRolesMapper()
    {
        if(!$this->rolesMapper)
        {die(print_r($this->serviceManager));
            $this->setRolesMapper($this->serviceManager->get('admin_role_mapper'));
        }
        return $this->rolesMapper;
    }

    /**
     * Retrieve service manager instance
     *
     * @return ServiceManager
     */
    public function getServiceManager()
    {
        return $this->serviceManager;
    }

    /**
     * Set service manager instance
     *
     * @param ServiceManager $serviceManager
     * @return User
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
        return $this;
    }

} 